package com.leha.mp.core.services.imp;

import com.leha.mp.core.Utils.SmsUtil;
import com.leha.mp.core.config.NoticeMessageConfig;
import com.leha.mp.core.constants.BizConstants;
import com.leha.mp.core.dao.CustomLogicMapper;
import com.leha.mp.core.dao.PlConsultingMapper;
import com.leha.mp.core.dao.PlConsultingNotifyRecordMapper;
import com.leha.mp.core.entity.mybatis.PlConsulting;
import com.leha.mp.core.entity.mybatis.PlConsultingNotifyRecord;
import com.leha.mp.core.entity.vo.ConsultingCronVo;
import com.leha.mp.core.entity.vo.UnConsultingCronVo;
import com.leha.mp.core.services.ConsultCronServices;
import com.leha.mp.core.services.HidingCallServices;

import com.leha.mp.core.services.PushServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author yanming
 *
 */
@Service
public class ConsultCronServiceImpl implements ConsultCronServices {

    private static final Logger LOG = LoggerFactory.getLogger(ConsultCronServiceImpl.class);

    @Autowired
    HidingCallServices hidingCallServices;

    @Autowired
    CustomLogicMapper customLogicMapper;

    @Autowired
    PlConsultingNotifyRecordMapper plConsultingNotifyRecordMapper;

    @Autowired
    PlConsultingMapper plConsultingMapper;

    @Autowired
    CommonServicesImpl commonServices;

    @Autowired
    PushServices pushServices;

    @Override
    public void checkConsultList() {

        LOG.info("cronjob check consulting list every minute");

        List<ConsultingCronVo> consultingCronVoList = customLogicMapper.getCronConsultingNotifyList();

        if (! consultingCronVoList.isEmpty()) {

            LOG.info("consulting is not empty");

            for (ConsultingCronVo consultingCronVo: consultingCronVoList) {
                LOG.info("begin notify consult id: " + consultingCronVo.getId() + " patient phone: "+ consultingCronVo.getDoctorPhone()+ " doctor phone: "+ consultingCronVo.getDoctorPhone());

                String voiceTemplate = String.format("%s您好，根据您预约的时间，%s医生会在10分钟后与您通话，请保持手机畅通。",
                        consultingCronVo.getPatientName(),
                        consultingCronVo.getDoctorName());
                this.sendVoiceSmsToPatient(consultingCronVo.getPatientPhone(), voiceTemplate);

                String smsMsg = NoticeMessageConfig.getSmsMessage(consultingCronVo.getPatientName(),
                                                                    consultingCronVo.getConsultingTime(),
                                                                    consultingCronVo.getConsultingType());
                this.sendSmsToDoctor(consultingCronVo.getDoctorPhone(), smsMsg);

                // 设置当前记录为已通知
                PlConsultingNotifyRecord plConsultingNotifyRecord = new PlConsultingNotifyRecord();
                plConsultingNotifyRecord.setConsultingId(consultingCronVo.getId());
                plConsultingNotifyRecord.setCreateTime(new Date());

                plConsultingNotifyRecordMapper.insert(plConsultingNotifyRecord);

                LOG.info("end notify consult id: " + consultingCronVo.getId());
            }
        } else {
            LOG.info("consulting list is empty");
        }

    }

    @Override
    public void checkUnConsultList() {
        List<UnConsultingCronVo> unConsultingCronVos = customLogicMapper.getCronUnConsultingNotifyList();

        if (! unConsultingCronVos.isEmpty()) {

            LOG.info("unconsulting list is not empty");

            for (UnConsultingCronVo unConsultingCronVo: unConsultingCronVos) {

                PlConsulting consulting = plConsultingMapper.selectByPrimaryKey(unConsultingCronVo.getConsultingId());

                LOG.info("begin try refund money for consult id "+ consulting.getConsultingId()+ " , order id:"+consulting.getOrderId());
                boolean refundResult = commonServices.refundMoneyForConsult(consulting, BizConstants.Consulting_Status_AutoClose);

                if (refundResult) {
                    pushServices.SendMessageToUserWithMp(consulting.getUserId(),NoticeMessageConfig.getConsultingExpiredMessage());
                }

                LOG.debug("begin refund money for consult id "+ consulting.getConsultingId()+ " , order id:"+consulting.getOrderId() + ", refund result: "+ (refundResult ? "success": "fail"));

            }
        } else {
            LOG.info("unconsulting list is empty");
        }
    }


    /*==========================================*/
    /* 即将开始的预约请求 */
    /*==========================================*/
    /**
     * 拨打电话语音给病人，通知病人预约咨询即将开始
     * @param phoneNumber String 病人电话号码
     */
    private void sendVoiceSmsToPatient(String phoneNumber, String message) {
        hidingCallServices.MakeLandingCallsWithCustomContent(phoneNumber, message);
    }

    /**
     * 发送短信给医生，提示医生预约即将开始
     * @param phoneNumber 电话号码
     * @param message 预约信息
     */
    private void sendSmsToDoctor(String phoneNumber, String message) {
        SmsUtil.send(phoneNumber, message);
    }

}
